package microarray;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Query  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3999043814628408962L;
	@Persistent
	private EFetch fetch;
	@Persistent
	private ESearch search;
	@Persistent
	private ESummary summary;
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
    @Persistent
	private String searchString;

    @Persistent
    private String fetchString;

    @Persistent
    private String summaryString;
    
    @Persistent 
    private String message;
	
	
	public Query(String term, String reldate, String retmax, String sortby, String db)
	{
		TreeMap<String, String> map = new TreeMap<String, String>();
		
		map.put("db", db);
		
		StringTokenizer tok = new StringTokenizer(term);
		try{
			String terms = tok.nextToken();
			String newTok;
			while(tok.hasMoreTokens())
			{
				newTok = tok.nextToken().replaceAll("\"", "");
				terms+="+AND+" + newTok;
			}
			map.put("term", terms);
		} catch (NoSuchElementException e){}
		
		try
		{
			int rel = Integer.parseInt(reldate);
			if (rel > 0)
				map.put("reldate", reldate);
		} catch (NumberFormatException n){}
		try
		{
			int max = Integer.parseInt(retmax);
			if (max > 0)
				map.put("retmax", retmax);
		} catch (NumberFormatException n){}
		map.put("usehistory","y");
		
		try{
			search = new ESearch(map);
			String webEnv = search.getWebEnv();
			String queryKey= search.getQueryKey();
			summary = new ESummary(queryKey, webEnv);
			fetch = new EFetch(queryKey, webEnv);
			
			searchString = search.toString();
			fetchString = fetch.toString();
			summaryString = summary.toString();
			
		} catch (Exception e){}
	}
	
 	public void setSearch( ESearch value ){search = value;}

    public void setFetch( EFetch value ){fetch = value;}

    public void setSummary( ESummary value ){summary = value;}
    
    public ESearch getSearch() { return search; }

    public EFetch getFetch() { return fetch; }

    public ESummary getSummary() { return summary; }
    
    
	
 	public void setSearchString( String value ){searchString = value;}

    public void setFetchString( String value ){fetchString = value;}

    public void setSummaryString( String value ){summaryString = value;}
    
    public Key getKey() {return key;}

    public String getSearchString() { return searchString; }

    public String getFetchString() { return fetchString; }

    public String getSummaryString() { return summaryString; }

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}
}
